package cn.com.forever.array;

/*
    快排讲解：https://www.runoob.com/w3cnote/sort-algorithm-summary.html
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] array = {2,6,4,8,1,10,34,7,5};
        quicksort(array,0,array.length-1);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }

    public static void quicksort(int[] arr,int low,int high){
        if(low >= high)
            return ;
        int i = low ;
        int j = high ;
        int key = arr[low];
        while(i < j){
            while(i < j && arr[j] > key)
                j--;
            if(i < j){
                arr[i] = arr[j];
                i++;
            }
            while(i < j && arr[i] < key){
                i++;
            }
            if(i < j){
                arr[j] = arr[i];
                j--;
            }
        }
        arr[i] = key;
        quicksort(arr,low,i-1);
        quicksort(arr,i+1,high);
    }
}
